package org.edu.modules.center.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.TimeStampUtil;
import org.edu.modules.center.entity.VeTeachNotice;
import org.edu.modules.center.service.IVeTeachNoticeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags={"发布教学通知"})
@RestController
@RequestMapping({"/center/veTeachNotice"})
public class VeTeachNoticeController
        extends BaseController<VeTeachNotice, IVeTeachNoticeService>
{
    private static final Logger log = LoggerFactory.getLogger(VeTeachNoticeController.class);
    @Autowired
    private IVeTeachNoticeService veTeachNoticeService;
    @Value("${edu.path.urlPre}")
    private String fileUrlPre;

    @AutoLog("发布教学通知-分页查询")
    @ApiOperation(value="分页查询", notes="发布教学通知-分页查询")
    @PostMapping({"/list"})
    @ApiImplicitParams({@io.swagger.annotations.ApiImplicitParam(name="title", value="标题", required=false), @io.swagger.annotations.ApiImplicitParam(name="tcid", value="课程id", required=false)})
    public Result<?> queryPageList(@ApiIgnore VeTeachNotice veTeachNotice, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeTeachNotice> queryWrapper = QueryGenerator.initQueryWrapper(veTeachNotice, req.getParameterMap());
        Page<Map<String, Object>> page = new Page(pageNo.intValue(), pageSize.intValue());
        queryWrapper.setEntity(veTeachNotice);
        IPage<Map<String, Object>> pageList = this.veTeachNoticeService.pageMaps(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog("发布教学通知-新增信息")
    @ApiOperation(value="新增信息", notes="发布教学通知-新增信息")
    @PostMapping({"/add"})
    public Result<?> add(@RequestBody VeTeachNotice veTeachNotice)
    {
        LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        veTeachNotice.setAdduser(Integer.valueOf(sysUser.getUserId()));
        Integer date = Integer.valueOf(TimeStampUtil.getNowSecondStamp());
        veTeachNotice.setAddtime(date);
        this.veTeachNoticeService.save(veTeachNotice);
        return Result.OK("添加成功！");
    }

    @AutoLog("发布教学通知-修改信息")
    @ApiOperation(value="修改信息", notes="发布教学通知-修改信息")
    @PostMapping({"/edit"})
    public Result<?> edit(@RequestBody VeTeachNotice veTeachNotice)
    {
        this.veTeachNoticeService.updateById(veTeachNotice);
        return Result.OK("编辑成功!");
    }

    @AutoLog("发布教学通知-通过id删除")
    @ApiOperation(value="通过id删除", notes="发布教学通知-通过id删除")
    @PostMapping({"/delete"})
    public Result<?> delete(@RequestParam(name="id", required=true) String id)
    {
        this.veTeachNoticeService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog("发布教学通知-通过id查询")
    @ApiOperation(value="通过id查询", notes="发布教学通知-通过id查询")
    @PostMapping({"/queryById"})
    public Result<?> queryById(@RequestParam(name="id", required=true) String id)
    {
        List<Map<String, Object>> maps = this.veTeachNoticeService.queryListById(id);
        if (maps == null) {
            return Result.error("未找到对应数据");
        }
        for (Map item : maps) {
            item.replace("sourceFile", this.fileUrlPre + item.get("sourceFile"));
        }
        return Result.OK(maps);
    }
}
